//
//  YFFileManager.m
//  YFFileMangerDemo
//
//  Created by yefei on 2019/4/24.
//  Copyright © 2019 yefei. All rights reserved.
//

#import "YFFileManager.h"

@implementation YFFileManager

#pragma mark - 绝对单例
static YFFileManager *_manager = nil;
+ (instancetype)shared {
    
    static dispatch_once_t onceToken = 0;
    dispatch_once(&onceToken, ^{
        _manager = [[[self class] alloc] init];
    });
    return _manager;
}

+ (instancetype)allocWithZone:(struct _NSZone *)zone {
    static dispatch_once_t onceToken = 0;
    dispatch_once(&onceToken, ^{
        _manager = [super allocWithZone:zone];
    });
    return _manager;
}

- (instancetype)copyWithZone:(NSZone *)zone {
    return _manager;
}

- (instancetype)mutableCopyWithZone:(NSZone *)zone {
    return _manager;
}
#pragma mark -
#pragma mark Doucments、Library、cahches Path
+ (NSString *)doucments {
    return NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).firstObject;
}

+ (NSString *)library {
    return NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES).firstObject;
}

+ (NSString *)caches {
    return NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES).firstObject;
}

+ (NSString *)home {
    return NSHomeDirectory();
}

+ (NSString *)temporary {
    return NSTemporaryDirectory();
}

+ (NSString *)applicationSupport {
    return NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, YES).firstObject;
}

#pragma mark -
#pragma mark - 判断文件目录/文件是否z存在
/**
 判断文件/文件夹是否存在
 // 需传全路径
 */
+(BOOL(^)(NSString *fullPath))exist {
    return ^BOOL(NSString *fullPath) {
        BOOL isExist = [NSFileManager.defaultManager fileExistsAtPath:fullPath];
        return isExist;
    };
};

#pragma mark -
#pragma mark - 创建文件目录
/**
 创建文件夹（目录）需要传全路径
 */
+ (NSString * _Nullable(^)(NSString *fullPath))createDirectory {
    
    return ^NSString * _Nullable(NSString *fullPath) { return YFFileManager.shared.createDirectory(fullPath);};
}

/**
 在Documents中创建文件目录
 */
+ (NSString * _Nullable(^)(NSString *path))createDirectoryInDocuments {
    return ^NSString * _Nullable(NSString *path) {
        NSString *fullPath = [YFFileManager.doucments stringByAppendingPathComponent:path];
        return YFFileManager.createDirectory(fullPath);
    };
}

/**
 在Library中创建文件目录
 */
+(NSString * _Nullable(^)(NSString *path))createDirectoryInLibrary {
    return ^NSString * _Nullable(NSString *path) {
        NSString *fullPath = [YFFileManager.library stringByAppendingPathComponent:path];
        return YFFileManager.createDirectory(fullPath);
    };
}

/**
 在Caches创建文件目录
 */
+(NSString * _Nullable(^)(NSString *path))createDirectoryInCaches {
    return ^NSString * _Nullable(NSString *path) {
        NSString *fullPath = [YFFileManager.caches stringByAppendingPathComponent:path];
        return YFFileManager.createDirectory(fullPath);
    };
}

/**
 在Temporary创建文件目录
 */
+(NSString * _Nullable(^)(NSString *path))createDirectoryInTemporary {
    return ^NSString * _Nullable(NSString *path) {
        NSString *fullPath = [YFFileManager.temporary stringByAppendingPathComponent:path];
        return YFFileManager.createDirectory(fullPath);
    };
}

+ (NSString * _Nullable(^)(NSString *path))createDirectoryInApplicationSupport {
    return ^NSString * _Nullable (NSString *path) {
        NSString *fullPath = [YFFileManager.applicationSupport stringByAppendingPathComponent:path];
        return YFFileManager.createDirectory(fullPath);
    };
}

/**
 创建路径
 */
- (NSString * _Nullable(^)(NSString *fullPath))createDirectory {
    
    return ^NSString *_Nullable(NSString *fullPath) {
        BOOL isSuccess = YES;
        BOOL isExist = YFFileManager.exist(fullPath);
        if (!isExist) {
            NSError *error;
            isSuccess = [NSFileManager.defaultManager createDirectoryAtPath:fullPath withIntermediateDirectories:YES attributes:nil error:&error];
            if (error) { NSAssert(NO, @"创建文件夹失败：%@" ,error.localizedDescription);}
        }
        return isSuccess ? fullPath : nil;
    };
}

#pragma mark -
#pragma mark - 创建文件
/**
 创建文件
 全路径
 指定文件名称
 
 */
+ (NSString *_Nullable(^)(NSString *fullPath, NSString *fileName))createFile {
    return ^NSString *_Nullable(NSString *fullPath, NSString *fileName) {
        return YFFileManager.shared.createFile(fullPath, fileName);
    };
}

/**
 在Documents下创建文件
 传文件名称
 */
+ (NSString *_Nullable(^)(NSString *fileName))createFileInDocuments {
    NSString *path = YFFileManager.doucments;
    return ^NSString *_Nullable(NSString *fileName) {
        return YFFileManager.createFile(path, fileName);
    };
}
/**
 创建文件
 全路径
 
 */
- (NSString *_Nullable(^)(NSString *fullPath, NSString *fileName))createFile {
    
    return ^NSString *_Nullable (NSString *fullPath, NSString *fileName) {
        
        BOOL isExist = YFFileManager.exist(fullPath);
        NSString *directoryPath = isExist ? fullPath : YFFileManager.createDirectory(fullPath);
        
        if (!directoryPath) {
            return nil;
        }
        
        NSString *filePath = [directoryPath stringByAppendingPathComponent:fileName];
        BOOL isSuccess = [NSFileManager.defaultManager createFileAtPath:filePath contents:nil attributes:nil];
        return isSuccess ? filePath : nil;
    };
}

#pragma mark -
#pragma mark - 删除
/**
 删除指定路径中的文件
 需传全路径
 */
+(BOOL(^)(NSString *fullPath, NSString *fileName))deleteFile {
    return ^BOOL (NSString *fullPath, NSString *fileName) {
        return [YFFileManager shared].deleteFile(fullPath, fileName);
    };
}
/**
 删除指定路径中的文件
 */
- (BOOL(^)(NSString *fullPath, NSString *fileName))deleteFile {
    
    return ^BOOL (NSString *fullPath, NSString *fileName) {
        
        NSString *filePath = [fullPath stringByAppendingPathComponent:fileName];
        NSError *error;
        BOOL isSuccess = [NSFileManager.defaultManager removeItemAtPath:filePath error:&error];
        if (error) {
            NSAssert(NO, @"删除文件失败：%@", error.localizedDescription);
        }
        return isSuccess;
    };
}

#pragma mark -
#pragma mark - 移动文件
/**
 移动文件
 */
+ (void(^)(NSString *fileName, NSString *fromPath, NSString *toPath))moveFile {
    
    return ^(NSString *fileName, NSString *fromPath, NSString *toPath) {
        return YFFileManager.shared.moveFile(fileName, fromPath, toPath);
    };
}
/**
 移动文件
 */
- (void(^)(NSString *fileName, NSString *fromPath, NSString *toPath))moveFile {
    
    return ^(NSString *fileName, NSString *fromPath, NSString *toPath) {
        
        if (!YFFileManager.exist(toPath)) {
            YFFileManager.createDirectory(toPath);
        }
        NSString *sourcePath = [fromPath stringByAppendingPathComponent:fileName];
        NSString *destinationPath = [toPath stringByAppendingString:fileName];
        
        NSError *error;
        [NSFileManager.defaultManager moveItemAtPath:sourcePath toPath:destinationPath error:&error];
        if (error) {
            NSAssert1(NO, @"移动文件失败：%@", error.localizedDescription);
        }
    };
}

@end
